﻿using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace MicroCloud.CodeGenerator.Migrations
{
    /// <inheritdoc />
    public partial class init : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AlterDatabase()
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "Gen_CodeProject",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "char(128)", maxLength: 128, nullable: false, comment: "编号", collation: "ascii_general_ci"),
                    Name = table.Column<string>(type: "VARCHAR(200)", maxLength: 200, nullable: false, comment: "项目描述")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    NamespacePrefix = table.Column<string>(type: "VARCHAR(200)", maxLength: 200, nullable: false, comment: "项目命名空间前缀，通常形如“公司.项目”")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Company = table.Column<string>(type: "VARCHAR(200)", maxLength: 200, nullable: true, comment: "公司")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Creator = table.Column<string>(type: "VARCHAR(200)", maxLength: 200, nullable: true, comment: "创建者")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Copyright = table.Column<string>(type: "VARCHAR(500)", maxLength: 500, nullable: true, comment: "版权信息")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    RootPath = table.Column<string>(type: "VARCHAR(200)", maxLength: 200, nullable: true, comment: "根目录")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    CreatedTime = table.Column<DateTime>(type: "datetime(6)", nullable: false, comment: "创建日期")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Gen_CodeProject", x => x.Id);
                },
                comment: "代码项目信息")
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "Gen_CodeTemplate",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "char(128)", maxLength: 128, nullable: false, comment: "编号", collation: "ascii_general_ci"),
                    Name = table.Column<string>(type: "VARCHAR(200)", maxLength: 200, nullable: false, comment: "Name")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    MetadataType = table.Column<int>(type: "int", nullable: false, comment: "MetadataType(MicroCloud.CodeGeneration.Generates.MetadataType：[0]Entity；[1]Module；[2]Project)"),
                    TemplateFile = table.Column<string>(type: "VARCHAR(500)", maxLength: 500, nullable: false, comment: "TemplateFile")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Order = table.Column<int>(type: "int", nullable: false, comment: "Order"),
                    OutputFileFormat = table.Column<string>(type: "VARCHAR(300)", maxLength: 300, nullable: false, comment: "OutputFileFormat")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    IsOnce = table.Column<bool>(type: "tinyint(1)", nullable: false, comment: "IsOnce"),
                    IsSystem = table.Column<bool>(type: "tinyint(1)", nullable: false, comment: "IsSystem"),
                    CreatedTime = table.Column<DateTime>(type: "datetime(6)", nullable: false, comment: "CreatedTime")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Gen_CodeTemplate", x => x.Id);
                },
                comment: "代码模板")
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "SYS_KeyValue",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", maxLength: 128, nullable: false, comment: "编号"),
                    Key = table.Column<string>(type: "VARCHAR(256)", maxLength: 256, nullable: false, comment: "数据键名")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    ValueJson = table.Column<string>(type: "longtext", nullable: false, comment: "数据值JSON")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    ValueType = table.Column<string>(type: "VARCHAR(512)", maxLength: 512, nullable: false, comment: "数据值类型名")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Display = table.Column<string>(type: "VARCHAR(256)", maxLength: 256, nullable: true, comment: "显示名称")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Remark = table.Column<string>(type: "VARCHAR(2048)", maxLength: 2048, nullable: true, comment: "备注")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    OrderNo = table.Column<decimal>(type: "decimal(22,10)", precision: 22, scale: 10, nullable: false, comment: "顺序编号"),
                    IsLocked = table.Column<bool>(type: "tinyint(1)", nullable: false, comment: "是否锁定"),
                    CreatedTime = table.Column<DateTime>(type: "datetime(6)", nullable: false, comment: "创建时间")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_SYS_KeyValue", x => x.Id);
                },
                comment: "键值数据")
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "Gen_CodeModule",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "char(128)", maxLength: 128, nullable: false, comment: "编号", collation: "ascii_general_ci"),
                    Name = table.Column<string>(type: "VARCHAR(200)", maxLength: 200, nullable: false, comment: "Name")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Display = table.Column<string>(type: "VARCHAR(200)", maxLength: 200, nullable: false, comment: "Display")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Order = table.Column<int>(type: "int", nullable: false, comment: "Order"),
                    CreatedTime = table.Column<DateTime>(type: "datetime(6)", nullable: false, comment: "CreatedTime"),
                    ProjectId = table.Column<Guid>(type: "char(128)", nullable: false, comment: "ProjectId", collation: "ascii_general_ci")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Gen_CodeModule", x => x.Id);
                    table.ForeignKey(
                        name: "FK_CodeModule_ProjectId",
                        column: x => x.ProjectId,
                        principalTable: "Gen_CodeProject",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                },
                comment: "代码模块信息")
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "Gen_CodeProjectTemplate",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "char(128)", maxLength: 128, nullable: false, comment: "编号", collation: "ascii_general_ci"),
                    IsLocked = table.Column<bool>(type: "tinyint(1)", nullable: false, comment: "IsLocked"),
                    ProjectId = table.Column<Guid>(type: "char(128)", nullable: false, comment: "ProjectId", collation: "ascii_general_ci"),
                    TemplateId = table.Column<Guid>(type: "char(128)", nullable: false, comment: "TemplateId", collation: "ascii_general_ci")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Gen_CodeProjectTemplate", x => x.Id);
                    table.ForeignKey(
                        name: "FK_CodeProjectTemplate_ProjectId",
                        column: x => x.ProjectId,
                        principalTable: "Gen_CodeProject",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_CodeProjectTemplate_TemplateId",
                        column: x => x.TemplateId,
                        principalTable: "Gen_CodeTemplate",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                },
                comment: "项目模板配对")
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "Gen_CodeEntity",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "char(128)", maxLength: 128, nullable: false, comment: "编号", collation: "ascii_general_ci"),
                    Name = table.Column<string>(type: "VARCHAR(200)", maxLength: 200, nullable: false, comment: "Name")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Display = table.Column<string>(type: "VARCHAR(200)", maxLength: 200, nullable: false, comment: "Display")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    PrimaryKeyTypeFullName = table.Column<string>(type: "VARCHAR(500)", maxLength: 500, nullable: false, comment: "PrimaryKeyTypeFullName")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Listable = table.Column<bool>(type: "tinyint(1)", nullable: false, comment: "Listable"),
                    Addable = table.Column<bool>(type: "tinyint(1)", nullable: false, comment: "Addable"),
                    Updatable = table.Column<bool>(type: "tinyint(1)", nullable: false, comment: "Updatable"),
                    Deletable = table.Column<bool>(type: "tinyint(1)", nullable: false, comment: "Deletable"),
                    HasCreatedTime = table.Column<bool>(type: "tinyint(1)", nullable: false, comment: "HasCreatedTime"),
                    HasLocked = table.Column<bool>(type: "tinyint(1)", nullable: false, comment: "HasLocked"),
                    HasSoftDeleted = table.Column<bool>(type: "tinyint(1)", nullable: false, comment: "HasSoftDeleted"),
                    HasCreationAudited = table.Column<bool>(type: "tinyint(1)", nullable: false, comment: "HasCreationAudited"),
                    HasUpdateAudited = table.Column<bool>(type: "tinyint(1)", nullable: false, comment: "HasUpdateAudited"),
                    Order = table.Column<int>(type: "int", nullable: false, comment: "Order"),
                    CreatedTime = table.Column<DateTime>(type: "datetime(6)", nullable: false, comment: "CreatedTime"),
                    ModuleId = table.Column<Guid>(type: "char(128)", nullable: false, comment: "ModuleId", collation: "ascii_general_ci")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Gen_CodeEntity", x => x.Id);
                    table.ForeignKey(
                        name: "FK_CodeEntity_ModuleId",
                        column: x => x.ModuleId,
                        principalTable: "Gen_CodeModule",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                },
                comment: "代码实体信息")
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "Gen_CodeForeign",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "char(128)", maxLength: 128, nullable: false, comment: "编号", collation: "ascii_general_ci"),
                    SelfNavigation = table.Column<string>(type: "varchar(512)", maxLength: 512, nullable: true, comment: "SelfNavigation")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    SelfForeignKey = table.Column<string>(type: "varchar(512)", maxLength: 512, nullable: true, comment: "SelfForeignKey")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    OtherEntity = table.Column<string>(type: "varchar(512)", maxLength: 512, nullable: true, comment: "OtherEntity")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    OtherNavigation = table.Column<string>(type: "varchar(512)", maxLength: 512, nullable: true, comment: "OtherNavigation")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    IsRequired = table.Column<bool>(type: "tinyint(1)", nullable: false, comment: "IsRequired"),
                    DeleteBehavior = table.Column<int>(type: "int", nullable: true, comment: "DeleteBehavior"),
                    ForeignRelation = table.Column<int>(type: "int", nullable: false, comment: "ForeignRelation(MicroCloud.CodeGeneration.Entities.ForeignRelation：[0]OneToOne；[1]OneToMany；[2]ManyToOne；[3]OwnsOne；[4]OwnsMany)"),
                    EntityId = table.Column<Guid>(type: "char(128)", nullable: false, comment: "EntityId", collation: "ascii_general_ci")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Gen_CodeForeign", x => x.Id);
                    table.ForeignKey(
                        name: "FK_CodeForeign_EntityId",
                        column: x => x.EntityId,
                        principalTable: "Gen_CodeEntity",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                },
                comment: "实体外键")
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "Gen_CodeProperty",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "char(128)", maxLength: 128, nullable: false, comment: "编号", collation: "ascii_general_ci"),
                    Name = table.Column<string>(type: "VARCHAR(200)", maxLength: 200, nullable: false, comment: "Name")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Display = table.Column<string>(type: "VARCHAR(200)", maxLength: 200, nullable: true, comment: "Display")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    TypeName = table.Column<string>(type: "VARCHAR(500)", maxLength: 500, nullable: false, comment: "TypeName")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    IsRequired = table.Column<bool>(type: "tinyint(1)", nullable: true, comment: "IsRequired"),
                    MinLength = table.Column<int>(type: "int", nullable: true, comment: "MinLength"),
                    MaxLength = table.Column<int>(type: "int", nullable: true, comment: "MaxLength"),
                    IsNullable = table.Column<bool>(type: "tinyint(1)", nullable: false, comment: "IsNullable"),
                    IsForeignKey = table.Column<bool>(type: "tinyint(1)", nullable: false, comment: "IsForeignKey"),
                    IsNavigation = table.Column<bool>(type: "tinyint(1)", nullable: false, comment: "IsNavigation"),
                    RelateEntity = table.Column<string>(type: "varchar(512)", maxLength: 512, nullable: true, comment: "RelateEntity")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    IsInputDto = table.Column<bool>(type: "tinyint(1)", nullable: false, comment: "IsInputDto"),
                    IsOutputDto = table.Column<bool>(type: "tinyint(1)", nullable: false, comment: "IsOutputDto"),
                    DefaultValue = table.Column<string>(type: "varchar(512)", maxLength: 512, nullable: true, comment: "DefaultValue")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Order = table.Column<int>(type: "int", nullable: false, comment: "Order"),
                    CreatedTime = table.Column<DateTime>(type: "datetime(6)", nullable: false, comment: "CreatedTime"),
                    EntityId = table.Column<Guid>(type: "char(128)", nullable: false, comment: "EntityId", collation: "ascii_general_ci")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Gen_CodeProperty", x => x.Id);
                    table.ForeignKey(
                        name: "FK_CodeProperty_EntityId",
                        column: x => x.EntityId,
                        principalTable: "Gen_CodeEntity",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                },
                comment: "代码实体属性")
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateIndex(
                name: "IX_Gen_CodeEntity_ModuleId",
                table: "Gen_CodeEntity",
                column: "ModuleId");

            migrationBuilder.CreateIndex(
                name: "IX_Gen_CodeForeign_EntityId",
                table: "Gen_CodeForeign",
                column: "EntityId");

            migrationBuilder.CreateIndex(
                name: "IX_Gen_CodeModule_ProjectId",
                table: "Gen_CodeModule",
                column: "ProjectId");

            migrationBuilder.CreateIndex(
                name: "IDX_CodeProject_Name",
                table: "Gen_CodeProject",
                column: "Name",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_Gen_CodeProjectTemplate_ProjectId",
                table: "Gen_CodeProjectTemplate",
                column: "ProjectId");

            migrationBuilder.CreateIndex(
                name: "IX_Gen_CodeProjectTemplate_TemplateId",
                table: "Gen_CodeProjectTemplate",
                column: "TemplateId");

            migrationBuilder.CreateIndex(
                name: "IX_Gen_CodeProperty_EntityId",
                table: "Gen_CodeProperty",
                column: "EntityId");

            migrationBuilder.CreateIndex(
                name: "IDX_CodeTemplate_Name",
                table: "Gen_CodeTemplate",
                column: "Name",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IDX_KeyValue_Key",
                table: "SYS_KeyValue",
                column: "Key",
                unique: true);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "Gen_CodeForeign");

            migrationBuilder.DropTable(
                name: "Gen_CodeProjectTemplate");

            migrationBuilder.DropTable(
                name: "Gen_CodeProperty");

            migrationBuilder.DropTable(
                name: "SYS_KeyValue");

            migrationBuilder.DropTable(
                name: "Gen_CodeTemplate");

            migrationBuilder.DropTable(
                name: "Gen_CodeEntity");

            migrationBuilder.DropTable(
                name: "Gen_CodeModule");

            migrationBuilder.DropTable(
                name: "Gen_CodeProject");
        }
    }
}
